<?php

namespace app\common\model;

use think\Cache;
use think\Model;

/**
 * 订单数据模型
 */
class Order extends Model
{
    public $fields = '*';
    public $where = [];
    public $order = ['create_time' => 'desc'];
    public $limit = false;

    /**
     * 创建订单
     *
     * @param string $lng 经度
     * @param string $lat 纬度
     */
    public static function createOrder($uid,$params)
    {
        // 检测班次信息
        $line_banc = db('line_banc')->where('id', $params['banc_id'])->find();
        empty($line_banc) && exception('没有找到班次信息');
        //当前班次已购票信息
        $ticket_num = $line_banc['ticket_num'];
        $have_ticket = db('order')->where(['banc_id'=>$params['banc_id'],'status'=>['in','0,1,2,3']])->count();
        // 检测发车时间
        $now = time();
        $now>$line_banc['depart_time'] && exception('当前班次已发车');
        $depart_stale = config('site.depart_stale');
        $bigen_stale = $line_banc['depart_time'] - ($depart_stale * 60);
        ($now>=$bigen_stale && $now<=$line_banc['depart_time']) && exception("发车前$depart_stale 分钟不可订票");
        //检测线路信息
        $line = db('line')->where('id', $line_banc['line_id'])->find();
        empty($line) && exception('没有找到线路信息');
        //价格计算
        if($params['type'] == 1){ //拼车
            $sharing_price = $ticket_price = $line['sharing_price'];
            $passenger_num = count(explode(',',$params['passenger_ids']));
            $pay_price = round($sharing_price * $passenger_num);
            //检测余票
            $be_have_ticket = $passenger_num + $have_ticket;
            $be_have_ticket>$ticket_num && exception('余票不足');
        }elseif($params['type'] == 2){ //包车
            $pay_price = $ticket_price = $line['all_price'];
            //检测余票
            $have_ticket>0 && exception('当前班次已有乘客拼车订票');
        }

        $sn = \fast\Random::alpha(6);
        $sn.= $params['type'] == 1 ? 'p' : 'b';
        $insData = [
            'uid' => $uid,
            'order_sn' => strtoupper($sn).date('YmdHis'),
            'type' => $params['type'],
            'banc_id' => $params['banc_id'],
            'banc_sn' => $line_banc['banc_sn'],
            'line_id' => $line_banc['line_id'],
            'line_name' => $line['line_name'],
            'ticket_price' => $ticket_price,
            'pay_price' => $pay_price,
            'mobile' => $params['mobile'],
            'passenger_ids' => $params['passenger_ids'] ?? '',
            'username' => $params['username'] ?? '',
            'idcard' => $params['idcard'] ?? '',
            'start_station_id' => $params['start_station_id'],
            'end_station_id' => $params['end_station_id'],
            'order_desc' => $params['order_desc'] ?? '',
            'depart_time' => $line_banc['depart_time'],
            'distance' => $line_banc['distance'],
            'distance_time' => $line_banc['distance_time'],
            'driver_id' => $line_banc['driver_id']
        ];
        //上车选点信息
        $up_car_coordinate = $params['up_car_coordinate'] ?? '';
        $up_car_address = $params['up_car_address'] ?? '';
        $insData['up_car_data'] = json_encode(compact('up_car_coordinate','up_car_address'));
        //下车选点信息
        $down_car_coordinate = $params['down_car_coordinate'] ?? '';
        $down_car_caddress = $params['down_car_caddress'] ?? '';
        $insData['down_car_data'] = json_encode(compact('down_car_coordinate','down_car_caddress'));
        //订单录入站点信息
        $station_info = db('line_station')
        ->where(['line_id'=>$line_banc['line_id'],'station_id'=>['in',[$params['start_station_id'],$params['end_station_id']]]])
        ->column('station_id,station_name,tags,coordinate_lat,coordinate_lng');
        $insData['start_station'] = $station_info[$params['start_station_id']]['station_name'];
        $insData['start_station_tags'] = $station_info[$params['start_station_id']]['tags'];
        $insData['start_station_coordinate'] = "{$station_info[$params['start_station_id']]['coordinate_lng']},{$station_info[$params['start_station_id']]['coordinate_lat']}";
        $insData['end_station'] = $station_info[$params['end_station_id']]['station_name'];
        $insData['end_station_tags'] = $station_info[$params['end_station_id']]['tags'];
        //过期时间
        $config_expirae_time = config('site.expirae_time');
        $config_expirae_time = $config_expirae_time * 60;
        $insData['expirae_time'] = $config_expirae_time + time();

        return self::create($insData);
    }


    public function _list()
    {
        $query = $this->field($this->fields)->where($this->where)->order($this->order);
        if ($this->limit === false) {
            $data = $query->select();
        } elseif($this->limit == 1) {
            $data = $query->find();
        } else {
            $data = $query->paginate($this->limit)->toArray();
        }

        return $data;
    }


}
